/**
 * 
 */
package com.school.homeServlet.cmd;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.school.dao.UserManagerDao;
import com.school.dao.UserManagerDaoImpl;
import com.school.entity.Login;
import com.school.homeServlet.cmd.UserLoginCommand;

/**
 * @author KK
 *
 */
public class UserLoginCommand implements HomeCommand {

	/* (non-Javadoc)
	 * @see com.school.homeServlet.cmd.HomeCommand#processRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.ServletConfig)
	 */
	public static Logger log = Logger.getLogger(UserLoginCommand.class.getSimpleName());
	@Override
	public void processRequest(HttpServletRequest req,
			HttpServletResponse resp, ServletConfig config)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		Login login = new Login();		//			log.info(req.getParameter("id"));	log.info(req.getParameter("password"));		log.info(req.getParameter("loginType"));
		login.setLoginId(req.getParameter("id"));
		login.setPassword(req.getParameter("password"));
		login.setType(req.getParameter("loginType"));
		
		UserManagerDao dao = new UserManagerDaoImpl();
		if(dao.verifyUser(login)){
			session.setAttribute("id", login.getLoginId());
			
			if(login.getType().equals("admin")){
				session.setAttribute("type", "admin");
				req.setAttribute("info", true);
				RequestDispatcher dispatcher = req.getRequestDispatcher("/admin_home.jsp");
				dispatcher.forward(req, resp);
				}
			else if(login.getType().equals("parent")){
				session.setAttribute("type", "parent");
				req.setAttribute("info", true);
				RequestDispatcher dispatcher = req.getRequestDispatcher("/parent_home.jsp");
				dispatcher.forward(req, resp);
				}
			else if(login.getType().equals("student")){
				session.setAttribute("type", "student");
				req.setAttribute("info", true);
				RequestDispatcher dispatcher = req.getRequestDispatcher("/student_home.jsp");
				dispatcher.forward(req, resp);
				}
			else if(login.getType().equals("teacher")){
				session.setAttribute("type", "teacher");
				req.setAttribute("info", true);
				RequestDispatcher dispatcher = req.getRequestDispatcher("/teacher_home.jsp");
				dispatcher.forward(req, resp);
				}		//End of inner else
		}			//End of outer "if"
		else{
	/*		NewsDao newsobject1 = new NewsDaoImpl();		
			ArrayList<News> schnews = newsobject1.viewSchoolNews();
			
			req.setAttribute("schoolnews", schnews);		//////////////////
	*/		
			req.setAttribute("loginError", true);
			RequestDispatcher dispatcher = req.getRequestDispatcher("/school_home.jsp");
			dispatcher.forward(req, resp);
			}		//End of outer "else"
	}			//End of "process request"

}			//End of "user login command" class
